<?php
namespace structure\components;

class Image extends \components\Component
{
	public function Start()
	{
		$comp = $this->GetComponent();

		$name = $comp->GetAttribute("name");
		$type = $comp->GetAttribute("type");
		$page = $comp->GetAttribute("page");

		$attrs = $comp->GetAttributes();

		$extraAttrs = array_diff($attrs, array("name"=>$name, "type"=>$type, "page"=>$page));

		$image = service("db")->
		Select("_images.*", "_images_types.extention", "_languages_content.ru AS alt", "_pages.file_name AS page")->
		From("_images")->
		Join("_images_types", "INNER")->
		On("_images_types.id", "=", "_images.type")->
		Join("_languages_content", "INNER")->
		On("_languages_content.id", "=", "_images.title")->
		Join("_pages", "INNER")->
		On("_pages.id", "=", "_images.page")->
		Where("_images_types.type", "=", "?")->
		Where("_images.file_name", "=", "?")->
		Where("_pages.file_name", "=", "?")->
		Placeholders($type, $name, $page)->
		Fetch("row")->
		Run();

		if (empty($image))
		{
			return false;
		}

		$el = core("Html")->CreateElement("img");

		$el->AddAttribute("width", $image['width']);
		$el->AddAttribute("height", $image['height']);
		$el->AddAttribute("alt", $image['alt']);
		$el->AddAttribute("src", "/images/$page/$image[file_name].$image[extention]");

		$el->AddAttributes($extraAttrs);

		return $el;
	}
}